package com.situ.mall.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.situ.mall.base.entity.Attachment;
import com.situ.mall.base.entity.Category;
import com.situ.mall.base.entity.Product;
import com.situ.mall.base.service.IAttachmentService;
import com.situ.mall.base.service.ICategoryService;
import com.situ.mall.base.service.IProductService;
import com.situ.mall.base.util.AvatarUtil;
import com.situ.mall.base.util.C;
import com.situ.mall.base.util.URLUtil;
import com.situ.mall.base.vo.LayuiTableVO;
import com.situ.mall.base.vo.ResVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author darling
 * @since 2023-03-18
 */
@Controller
@RequestMapping(URLUtil.PRODUCT)
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IAttachmentService attachmentService;


    // 商品详情页
    @GetMapping(URLUtil.PAGE)
    public String page() {
        return URLUtil.PRODUCT_HTML;
    }

    // 商品列表
    @GetMapping(URLUtil.LIST)
    @ResponseBody
    public LayuiTableVO list(String keyword, Integer page, Integer limit) {
        return productService.doList(keyword, page, limit);
    }

    // 已删除商品列表
    @GetMapping(URLUtil.DELETE + URLUtil.LIST)
    @ResponseBody
    public LayuiTableVO delList(String keyword, Integer page, Integer limit) {
        return productService.doDelList(keyword, page, limit);
    }

    // 修改或新增时的保存
    @PostMapping(URLUtil.SAVE)
    @ResponseBody
    public void save(Product product) {
        productService.saveOrUpdate(product);
    }

    // 获取欲修改的商品信息
    @PostMapping(URLUtil.EDIT + URLUtil.UID)
    @ResponseBody
    public Product edit(@PathVariable(C.ID) Integer id) {
        Product p = productService.getById(id);
        p.setTopCategory(categoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getId, p.getCateId())).getParentId());
        return p;
    }

    // 上传附件
    @PostMapping(URLUtil.IMAGES)
    @ResponseBody
    public ResVO upload(MultipartFile file) {
        return attachmentService.upload(file);
    }

    // 保存附件
    @PostMapping(URLUtil.ATTR)
    @ResponseBody
    public void attr(String ids, Integer pId){
        attachmentService.updateSource(ids, pId);
    }

    @GetMapping(URLUtil.ATTACHMENT + URLUtil.UID)
    @ResponseBody
    public List<Attachment> attachments(@PathVariable("id") Integer pId){
        return attachmentService.getList(pId, "product");
    }

    // 首页图
    @PostMapping(URLUtil.FIRST_IMAGE)
    @ResponseBody
    public ResVO avatar(MultipartFile file) {
        return productService.avatar(file);
    }

    // 批量上下架
    // ---- 批量上架
    @PostMapping(URLUtil.START)
    @ResponseBody
    public void start(@RequestParam("ids[]") Integer[] ids) {
        productService.doStartOrStop(ids, 1);
    }

    // 批量上下架
    // ---- 批量下架
    @PostMapping(URLUtil.STOP)
    @ResponseBody
    public void stop(@RequestParam("ids[]") Integer[] ids) {
        productService.doStartOrStop(ids, 0);
    }

    // 删除商品
    // ---- 单个删除
    @PostMapping(URLUtil.DELETE)
    @ResponseBody
    public void delete(Integer id) {
        productService.doDel(id);
    }

    // 删除商品
    // ---- 批量删除
    @PostMapping(URLUtil.DELETE_ALL)
    @ResponseBody
    public void deleteAll(@RequestParam("ids[]") Integer[] ids) {
        productService.doDelAll(ids);
    }


}
